﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using UnityEngine;

namespace Assets.Scripts.FarmScene.UI
{
    public class UIManager
    {
        private static UIManager instance;
        public static UIManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new UIManager();
                return instance;
            }
        }
        private Dictionary<string, string> pathDict;
        //预制体缓存字典
        private Dictionary<string, GameObject> prefabDict;
        //已打开界面的缓存字典
        public Dictionary<string, BasePanel> panelDict;
        private UIManager()
        {
            InitDicts();
        }
        private void InitDicts()
        {
            prefabDict = new Dictionary<string, GameObject>();
            panelDict = new Dictionary<string, BasePanel>();
            pathDict = new Dictionary<string, string>()
            {
                { UIConst.panel1,"panel1"},
                { UIConst.panel2,"panel2"},
            };
        }
        private Transform uiRoot;
        public Transform UIRoot
        {
            get
            {
                if (GameObject.Find("Canvas"))
                {
                    uiRoot = GameObject.Find("Canvas").transform;
                }
                else
                {
                    uiRoot = new GameObject("Canvas").transform;
                }
                return uiRoot;
            }
        }
        public BasePanel OpenPanel(string name)
        { 
            BasePanel panel = null;
            //检查是否已经打开
            if (panelDict.TryGetValue(name, out panel))
            {
                Debug.LogError("界面已打开：" + name);
                return null;
            }
            //检查路径是否配置
            string path = "";
            if (!pathDict.TryGetValue(name, out path))
            {
                Debug.LogError("界面名称错误，或未配置路径: " + name);
                return null;
            }
            //使用缓存预制体
            GameObject panelPrefab = null;
            if (!prefabDict.TryGetValue(name, out panelPrefab))
            {
                string realPath = "Prefab/UIPanel/" + path;
                panelPrefab=Resources.Load<GameObject>(realPath)as GameObject;
                prefabDict.Add(name, panelPrefab);
            }
            //打开界面
            GameObject panelObject=GameObject.Instantiate(panelPrefab,UIRoot,false);
            panel =panelObject.GetComponent<BasePanel>();
            panelDict.Add(name, panel);
            panel.OpenPanel(name);
            return panel;
        }

        public bool ClosePanel(string name)
        { 
            BasePanel panel=null;
            if (!panelDict.TryGetValue(name, out panel))
            {
                Debug.LogError("界面未打开" + name);
                return false;
            }
            panel.ClosePanel(name);
            return true;
        }
      
    }
    public class UIConst
    {
        public const string panel1 = "panel1";
        public const string panel2 = "panel2";
    }
}
